/*
 * @Author: Setsuna
 * @Email: setsuna.xyz@qq.com
 * @FilePath: \src\index.tsx
 * @Date: 2022-12-05 10:44:43
 * @LastEditTime: 2023-03-17 14:09:32
 * @LastEditors: Setsuna
 */
import 'react-app-polyfill/ie11';
import 'react-app-polyfill/stable';
import React from 'react';
import ReactDOM from 'react-dom';
import './style/style.scss';
import App from './App';
import * as serviceWorker from './serviceWorker';
import { IUaaAppStartOptions } from './lib/types';
import { routes } from './config/routes';
import { udConfigProvider } from './lib/core/udConfigProvider';
import { menus } from './config/menus';
import { uaaApp } from './lib/core/uaaApp';
import Login from './pages/Login';
import udMainStore from './store/UdMain';
// const { antdThemeChanger } = require('@ud/antd-theme/dist');
// import '@ud/antd-theme';

// ReactDOM.render(<App />, document.getElementById('root'));

/* 员工管理端header需要传type:2

 */
udConfigProvider.http.requestBefore = (config) => {
  // const brandCode = JSON.parse(sessionStorage.getItem('UaaSysInfo'))?.profile?.brandCode
  // const code = JSON.parse(sessionStorage.getItem('UaaSysInfo'))?.profile?.code
  config = { ...config, headers: { ...config.headers, accountType: 0 } };
  return config;
};

if (process.env.REACT_APP_ENV == 'local') {
  // require("../mock")
}

let options: IUaaAppStartOptions = {
  appCode: 'LZLJ',
  routes: routes,
  loginComponent: Login,
  success: () => {
    udConfigProvider.api.useConditionsField = false;
    if (window.location.hash === '#/' || !window.location.hash) {
      // 如果没有hash数据，或者#/时，获取默认第一个菜单进行跳转
      const menuKey = uaaApp.menus[0].children && uaaApp.menus[0].children.length ? uaaApp.menus[0].children[0].key : uaaApp.menus[0].key;
      const path = uaaApp.routesMaps[menuKey]?.path;
      window.location.hash = path as string;
    }
    ReactDOM.render(<App />, document.getElementById('root'));
  },
};

if (process.env.REACT_APP_ENV == 'local') {
  options.local = {
    useLocalMenu: false,
    menus: menus,
    apiBaseUrl: 'http://10.0.48.28:9999',
    loginApiBaseUrl: 'http://10.0.48.28:9999',
    //apiBaseUrl: 'https://jxsmhtest03.lzlj.com',
    //loginApiBaseUrl: 'https://jxsmhtest03.lzlj.com',
    username: 'admin',
    password: 'Aa888888',
  };
}
const sysInfo: any = uaaApp.getSysInfo();
if (sysInfo) {
  const brandList = sysInfo.profile.brandList?.map(({ brandCode: value, brandName: title, organizationId }) => ({ value, title, organizationId }));
  udMainStore.brandList = brandList;
  udMainStore.initialBrandValue = brandList && brandList[0] ? brandList[0].value : undefined;
}

uaaApp.start(options);
